package com.junyidao.game.service;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.junyidao.game.Exception.BusinessException;
import com.junyidao.game.constant.BaseConstant;
import com.junyidao.game.constant.SysUserConstant;
import com.junyidao.game.core.LoginInfo;
import com.junyidao.game.mapper.SysRoleMapper;
import com.junyidao.game.mapper.SysRolePrivRelaMapper;
import com.junyidao.game.pojo.SysRole;
import com.junyidao.game.pojo.SysRolePrivRela;
import com.junyidao.game.pojo.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zzh on 2016/8/27.
 */
@Service
@Transactional
public class RoleService {

    @Autowired
    private SequenceService sequenceService;

    @Autowired
    private SysRoleMapper roleMapper;

    @Autowired
    private SysRolePrivRelaMapper sysRolePrivRelaMapper;

    public List<SysRole> getAll() {
        SysRole role = new SysRole();
        role.setStatuscode(BaseConstant.STATUS_NORMAL);
        List<SysRole> list = roleMapper.select(role);
        return list;
    }

    public Map<String, Object> getAll(SysRole role) {
        PageHelper.startPage(role.getPage(), role.getRows());
        List<SysRole> list = roleMapper.selectSysRole(role);
        PageInfo page = new PageInfo(list);
        Map<String, Object> map = new HashMap<>();
        map.put("list", list);
        map.put("count", page.getTotal());
        return map;
    }


    public List<SysRole> getRoleByUid(Integer uid) {
        Map<String, Object> map = new HashMap<>();
        map.put("uid", uid);
        List<SysRole> list = roleMapper.getRoleByUid(map);
        return list;
    }


    public void activate(String roleIds, String statuscode) {
        List<Integer> ids = JSONArray.parseArray(roleIds, Integer.class);
        Map<String, Object> map = new HashMap<>();
        map.put("roleIds", ids);
        map.put("statuscode", statuscode);
        roleMapper.activateRole(map);


    }

    public void addRole(SysRole role, String privIds) {
        SysRole sysRole = new SysRole();
        sysRole.setRoleName(role.getRoleName());
        List<SysRole> list = roleMapper.select(sysRole);
        if (list.size() > 0) {
            BusinessException.throwException(1, "角色已存在，请修改角色名");
        }
        Integer id = sequenceService.getNextSequence("USER_ID");
        role.setRoleId(id);
        role.setStatuscode(BaseConstant.STATUS_NORMAL);
        role.setRoleType("0");
        roleMapper.insert(role);
        insertRolePriv(privIds, id);

    }

    public void insertRolePriv(String privIds, Integer roleId) {
        if (!StringUtils.isEmpty(privIds)) {
            List<Integer> idList = JSONArray.parseArray(privIds, Integer.class);
            if (idList.size() > 0) {
                for (Integer privId : idList) {
                    Integer id = sequenceService.getNextSequence("ROLE_PRIV_RELA_ID");
                    SysRolePrivRela rela = new SysRolePrivRela();
                    rela.setRolePrivRelaId(id);
                    rela.setRoleId(roleId);
                    rela.setPrivId(privId);
                    sysRolePrivRelaMapper.insert(rela);
                }
            }
        }
    }

    public void updateSysRole(SysRole sysRole, String privIds) {
        SysRole role = new SysRole();
        role.setRoleName(sysRole.getRoleName());
        List<SysRole> roleList = roleMapper.select(role);
        if (roleList.size() > 1) {
            BusinessException.throwException(1, "角色已存在，请修改角色名");
        } else if (roleList.size() == 1) {
            if (!roleList.get(0).getRoleId().equals(sysRole.getRoleId())) {
                BusinessException.throwException(1, "角色名已存在，请修改角色名");
            }
        }
        roleMapper.updatePartByPrimaryKey(sysRole);

        SysRolePrivRela rela = new SysRolePrivRela();
        rela.setRoleId(sysRole.getRoleId());
        sysRolePrivRelaMapper.delete(rela);

        insertRolePriv(privIds, sysRole.getRoleId());

    }

    public SysRole selectByPrimaryKey(Integer roleId) {
        SysRole sysRole = new SysRole();
        sysRole.setRoleId(roleId);
        List<SysRole> list = roleMapper.select(sysRole);
        return list.size() > 0 ? list.get(0) : null;
    }
}
